package com.megalith.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.megalith.constant.ResultCode;
import com.megalith.dto.CandidateUserDTO;
import com.megalith.service.ProcessEngineUserService;
import com.megalith.vo.Result;
import com.megalith.vo.UserVO;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.User;
import org.flowable.idm.api.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author lfp
 * @Date 2022/7/16
 * @Version 1.0
 */
@Service
public class ProcessEngineUserServiceImpl implements ProcessEngineUserService {

    @Autowired
    private IdentityService identityService;

    @Override
    public Result<User> addUser(CandidateUserDTO userDTO) {
        if(StrUtil.isNotBlank(userDTO.getUserId()) && ifHaveUser(userDTO.getUserId())){
            return Result.sysError("用户ID已存在！");
        }

        User user = identityService.newUser(generateUserId(userDTO.getUserId()));
        user.setFirstName(userDTO.getFirstName());
        user.setLastName(userDTO.getLastName());
        user.setEmail(userDTO.getEmail());
        user.setPassword(userDTO.getPassword());

        identityService.saveUser(user);
        return Result.ok(user);
    }

    @Override
    public Result deleteUser(String userId) {
        if(!ifHaveUser(userId)){
            return Result.sysError("未查询到该用户");
        }

        identityService.deleteUser(userId);//删除用户时，会级联删除其关联的用户组数据
        return Result.ok(ResultCode.SYS_SUCCESS.getDes());
    }

    @Override
    public List<UserVO> listUser(CandidateUserDTO userDTO) {
        UserQuery query = identityService.createUserQuery();
        if(StrUtil.isNotBlank(userDTO.getUserId())){
            query.userIdIgnoreCase(userDTO.getUserId());
        }
        if(StrUtil.isNotBlank(userDTO.getFirstName())){
            query.userFirstNameLikeIgnoreCase(userDTO.getFirstName());
        }
        if(StrUtil.isNotBlank(userDTO.getLastName())){
            query.userLastNameLikeIgnoreCase(userDTO.getLastName());
        }
        if(StrUtil.isNotBlank(userDTO.getEmail())){
            query.userEmailLike(userDTO.getEmail());
        }
        List<User> list = query.orderByUserFirstName().asc().orderByUserId().asc().list();
        List<UserVO> userVOList = list.stream().map(user -> {
            return BeanUtil.copyProperties(user, UserVO.class);
        }).collect(Collectors.toList());

        return userVOList;
    }

    /**
    *@Description 判断用户是否存在
    *@Author lfp
    *@Date 2022/7/16
    *@param userId
    *@Return 
    */
    public boolean ifHaveUser(String userId){
        List<User> list = identityService.createUserQuery()
                .userId(userId)
                .list();
        return list.size()>0;
    }

    //获取用户id
    public String generateUserId(String userId){
        if(StrUtil.isBlank(userId)){
            userId = UUID.randomUUID().toString().replace("-","");
        }
        return userId;
    }
}
